package com.tz.cartelo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tz.cartelo.R;
import com.tz.cartelo.model.GoodsList;
import com.tz.cartelo.util.CommonUtils;

import java.util.List;


/**
 * Created by ok on 2016/9/18.
 */
public class GoodsCenterFragmentAdapter extends BaseAdapter {
    private LayoutInflater mLayoutInflater;
    private List<GoodsList> mGoodsListList;
    private Context mContext;
    private int mImgWidth, mImgHeight;

    public GoodsCenterFragmentAdapter(Context context, List<GoodsList> goodsListList) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        mGoodsListList = goodsListList;
//        setImgSize();
    }

    private void setImgSize() {
        mImgWidth = (CommonUtils.getScreenWidth(mContext) - 3 * mContext.getResources().getDimensionPixelOffset(R.dimen.margin_10)) / 2;
        mImgHeight = mImgWidth;
        //+ mContext.getResources().getDimensionPixelOffset(R.dimen.margin_5)
    }

    @Override
    public int getCount() {
        return mGoodsListList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = mLayoutInflater.inflate(R.layout.item_goods_center_fragment, null);
            holder = new ViewHolder();
            holder.itemGoodsCenterImg = (ImageView) view.findViewById(R.id.item_goods_center_img);
            holder.itemGoodsCenterNameText = (TextView) view.findViewById(R.id.item_goods_center_name_text);
            holder.itemGoodsCenterPriceText = (TextView) view.findViewById(R.id.item_goods_center_price_text);
            holder.itemGoodsCenterStockText = (TextView) view.findViewById(R.id.item_goods_center_stock_text);
            holder.itemGoodsCenterLeftView = view.findViewById(R.id.item_goods_center_left_view);
            holder.itemGoodsCenterRightView = view.findViewById(R.id.item_goods_center_right_view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

//        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(mImgWidth, mImgHeight);
//        holder.itemGoodsCenterImg.setLayoutParams(layoutParams);
        if (i % 2 == 0) {
            holder.itemGoodsCenterLeftView.setVisibility(View.VISIBLE);
            holder.itemGoodsCenterRightView.setVisibility(View.GONE);
        } else {
            holder.itemGoodsCenterLeftView.setVisibility(View.GONE);
            holder.itemGoodsCenterRightView.setVisibility(View.VISIBLE);
        }

//        ImageLoader.getInstance().displayImage("drawable://" + R.mipmap.ic_holder_list, holder.itemGoodsCenterImg, CommonUtils.optionsList());

        ImageLoader.getInstance().displayImage(mGoodsListList.get(i).getGoodsPicture(), holder.itemGoodsCenterImg, CommonUtils.optionsList());
        holder.itemGoodsCenterNameText.setText(mGoodsListList.get(i).getTitle());
        holder.itemGoodsCenterPriceText.setText("¥" + mGoodsListList.get(i).getPrice());
        holder.itemGoodsCenterStockText.setText(mContext.getResources().getString(R.string.goods_center_total_stock)
                + mGoodsListList.get(i).getTotalNum() + mContext.getResources().getString(R.string.goods_center_total_stock_unit));

        return view;
    }

    private class ViewHolder {
        ImageView itemGoodsCenterImg;
        TextView itemGoodsCenterNameText;
        TextView itemGoodsCenterPriceText;
        TextView itemGoodsCenterStockText;
        View itemGoodsCenterLeftView;
        View itemGoodsCenterRightView;
    }

}
